package com.hk.controller;


import cn.hutool.aop.aspects.TimeIntervalAspect;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.db.transaction.TransactionLevel;
import cn.hutool.extra.spring.SpringUtil;
import com.hk.annotation.CommonParamValidate;
import com.hk.annotation.OpenApiRateLimiter;
import com.hk.annotation.OpenApiSign;
import com.hk.domain.dto.DateTypeDTO;
import com.hk.domain.dto.TaxTypeDTO;
import com.hk.domain.dto.UserDTO;
import com.hk.domain.entity.SysUser;
import com.hk.enums.SexEnum;
import com.hk.service.business.BatchDeclarationTypeBusiness;
import com.hk.service.business.LoginBusiness;
import com.hk.service.demo.LoginServcie;
import com.hk.service.profile.MessageService;
import com.hk.utils.R;
import com.hk.utils.SpringContextUtil;
import com.hk.utils.ValidatorUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/user")
@Api(tags = "用户模板")
@Slf4j
public class LoginController {

    @Autowired
    BatchDeclarationTypeBusiness business;

    @Autowired(required = false)
    private MessageService messageService;

    @Autowired
    private LoginServcie loginServcie;


    @ApiOperation("Spring Security-login demo")
    @PostMapping("login")
    public R login(@RequestBody SysUser sysUser) {
        return loginServcie.login(sysUser);
    }

    /**
     * 有test权限才能访问此接口
     * @return
     */
    @GetMapping("hello")
    @PreAuthorize("hasAuthority('test')")
    public R hello() {
        return R.ok("哈哈中");
    }

    @GetMapping("logout")
    public R logout() {
        return loginServcie.logout();
    }


    @ApiOperation("登录接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功")
    })
    @PostMapping("/login2")
//    @OpenApiSign()
    @OpenApiRateLimiter(limitNum = 2)
    public R login(@RequestBody UserDTO userDTO) {
        ValidatorUtils.validateEntity(userDTO);
        LoginBusiness.login(userDTO);
        return R.ok();
    }


    @PostMapping("/login3")
    @CommonParamValidate
    public R login3(@RequestBody UserDTO userDTO) {
        LoginBusiness.login(userDTO);
        return R.ok();
    }

    @PostMapping("/demo")
    public R dateType(@RequestBody DateTypeDTO dateTypeDTO){
        System.out.println(".......");
        return R.ok();
    }

    @ApiOperation("税种及税种代码")
    //枚举这样写文档
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "返回成功", response = TaxTypeDTO.class, responseContainer = "List")
//    })
    @GetMapping("/all")
    public R taxTypeAll(Integer sex) throws UnsupportedEncodingException {
//        business.processEnum(sex);

        Base64 base64 = new Base64();
        String pwd = new String(base64.decode("30a1b9c90776d5feea7cf04fed73d990"), "UTF-8");
        return R.ok().put("pwd", pwd);
    }


    @GetMapping("/getProfile")
    public void getEnvMesg() {
        String msg = messageService.getMsg();
        System.out.println(msg);

    }

    @GetMapping("/getBean")
    @SuppressWarnings("all")  //抑制所有警告，例如变量未使用等
    public void getBeanTest() {
//        LoginBusiness bean = SpringContextUtil.getBean(LoginBusiness.class);
//        bean.login(new UserDTO());
        //hutoo工具
//        LoginBusiness bean1 = SpringUtil.getBean(LoginBusiness.class);
//        System.out.println(bean1);

//        UserDTO userDTO = ReflectUtil.newInstance(UserDTO.class);

        //校验电话号码
//        boolean mobile = PhoneUtil.isMobile("12324");
//        System.out.println(mobile);
        MessageService bean = SpringContextUtil.getBean(MessageService.class);
        String msg = bean.getMsg();
//        business.exceptionDemo();
        log.info("MessageServiceImpl print msg {}", msg);
        System.out.println(msg);

    }

    /**
     * 异常情况
     */
    @GetMapping("/exception")
    public void exceptionDemo() {
        business.exceptionDemo();
    }


    @GetMapping("/sex")
    public void sex(SexEnum sexEnum) {
        log.info("sexEnum~~~~{}", sexEnum);
    }


    @GetMapping("/testFactory")
    public void testFactory() throws Throwable {
        business.testFactory();
    }

    @GetMapping("/testFactory2")
    public void testFactory2() throws Throwable {
        business.testFactory2();
    }



}
